<?php

namespace App\Http\Controllers\Admin;

use App\Models\HelpCategory;
use App\Requests\Admin\Help\HelpCategoryRequest;
use App\Traits\CurdTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HelpCategoryController extends BaseController
{
    use CurdTrait;

    /**
     * @var HelpCategory
     */
    protected $curdModel;

    public function _init()
    {
        parent::_init();
        $this->setCurdModel(new HelpCategory());
    }

    /**
     * 列表
     * @param \Illuminate\Http\Request $request
     */
    public function list(Request $request)
    {
        $paginate = $this->curdModel
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($this->pageSize)
            ->toArray();

        $list = $paginate['data'] ?? [];
        $count = $paginate['total'];

        return $this->responseSuccess('', [
            'list' => $list,
            'total' => $count,
        ]);
    }

    /**
     * 详情
     * @param \Illuminate\Http\Request $request
     */
    public function info(Request $request)
    {
        $id = $request->input('id');
        $model = HelpCategory::query()->find($id);
        if (empty($model)) {
            return $this->responseFail('数据不存在');
        }

        return $this->responseSuccess('', $model);
    }

    /**
     * 保存
     * @param \Illuminate\Foundation\Http\FormRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(HelpCategoryRequest $request)
    {
        $id = $request->input('id');
        if ($id) {
            return $this->edit($request);
        } else {
            return $this->add($request);
        }
    }

    /**
     * 添加
     * @param \Illuminate\Foundation\Http\FormRequest $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function add(HelpCategoryRequest $request)
    {
        $saveData = $request->validated();

        DB::beginTransaction();

        $row = $this->curdModel->create($saveData);
        if (!$row) {
            return $this->responseFail('添加失败');
        }

        DB::commit();

        return $this->responseSuccess('添加成功', ['id' => $row->getKey()]);
    }

    /**
     * 编辑
     * @param \Illuminate\Foundation\Http\FormRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function edit(HelpCategoryRequest $request)
    {
        $id = $request->input('id');

        $row = HelpCategory::findById($id);
        if (empty($row)) {
            return $this->responseFail('数据不存在');
        }

        $saveData = $request->validated();
        unset($saveData['id']);

        DB::beginTransaction();

        $res = $row->fill($saveData)->save();
        if (!$res) {
            return $this->responseFail('编辑失败');
        }

        DB::commit();

        return $this->responseSuccess('编辑成功', ['id' => $row->getKey()]);
    }

    public function all(Request $request)
    {
        return $this->responseSuccess('', HelpCategory::getAll());
    }
    /**
     * 删除
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id');

        $row = HelpCategory::findById($id);
        if (empty($row)) {
            return $this->responseFail('数据不存在');
        }

        DB::beginTransaction();

        if (!$row->delete()) {
            return $this->responseFail('删除失败');
        }

        DB::commit();

        return $this->responseSuccess('删除成功');
    }


}
